import { HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Inject, Injectable, Optional } from '@angular/core';

@Injectable()
export class UniversalInterceptor implements HttpInterceptor {
    constructor(@Optional() @Inject('serverUrl') protected serverUrl?: string | undefined) {}

    intercept(req: HttpRequest<any>, next: HttpHandler) {
        const serverReq = !this.serverUrl
            ? req
            : req.clone({
                  url: `${this.serverUrl}${req.url}`
              });

        return next.handle(serverReq);
    }
}
